﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
namespace OneShoppingUtility
{
    public class UtilityJson
    {
        public UtilityJson()
        {
           
        }
        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        private static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 格式化字符型、日期型、布尔型
        /// </summary>
        private static string StringFormat(string str, Type type)
        {
            if (type == typeof(string))
            {
                str = String2Json(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            else if (type != typeof(string) && string.IsNullOrEmpty(str))
            {
                str = "\"" + str + "\"";
            }
            return str;
        }
         /// <summary>
        /// List转换成Json 
        /// </summary>
        public static string ListToJson<T>(IList<T> list)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    Json.Append(ObjectToJson(list[i]));
                    if(i!=list.Count-1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]");
            return Json.ToString();
        }
  
        public static string DataTableJson(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
        
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            if (dt.Rows.Count == 0)
            {

            }
            else
            {
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            }

            jsonBuilder.Append("]");
            return jsonBuilder.ToString();
        }
        public static string ObjectToJson<T>(T obj)
        {
            StringBuilder json = new StringBuilder();
            json.Append("");
            Type type = obj.GetType();
            PropertyInfo[] pis = type.GetProperties();
            json.Append("{");
            for (int j = 0; j < pis.Length; j++)
            {
                json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(obj, null) + "\"");
                if (j < pis.Length - 1)
                {
                    json.Append(",");
                }
            }
            json.Append("}");
            return json.ToString();
        }
        public static string DatasetToJson(DataSet ds)
        {
            StringBuilder json = new StringBuilder();
            foreach (DataTable dt in ds.Tables)
            {
                json.Append("{\"");
                json.Append("total");
                json.Append("\":");
                json.Append(ds.Tables[0].Rows.Count);
                json.Append(",");
                json.Append(DataTableJson(ds.Tables[0]));
                json.Append("}");
            }
            return json.ToString();
        }
        public static string TotalJson(int totalCount, DataSet ds)
        {
            StringBuilder json = new StringBuilder();
            foreach (DataTable dt in ds.Tables)
            {
                json.Append("{\"");
                json.Append("total");
                json.Append("\":");
                json.Append(totalCount);
                json.Append(",rows"+"\":");
                json.Append(DataTableJson(ds.Tables[0]));
                json.Append("}");
            }
            return json.ToString();
        }


        /// <summary>
        /// 将协议和数据合并成json格式的字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="protocol">协议</param>
        /// <param name="list">list集合</param>
        /// <returns></returns>
        public static string ResListToJson<T>(int protocol, IList<T> list)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"");
            json.Append("result");
            json.Append("\":");
            json.Append("\""+protocol+"\"");
            json.Append(",\"data" + "\":");
            json.Append(ListToJson(list));
            json.Append("}");
            return json.ToString();
        }
        /// <summary>
        /// 将协议和实体合并成json格式的字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="protocol">协议</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public static string ResEntityToJson<T>(int protocol, T entity)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"");
            json.Append("result");
            json.Append("\":");
            json.Append("\""+protocol+"\"");
            json.Append(",\"data" + "\":");
            json.Append(ObjectToJson(entity));
            json.Append("}");
            return json.ToString();
        }
        /// <summary>
        /// 返回协议
        /// </summary>
        /// <param name="protocol">协议</param>
        /// <returns></returns>
        public static string ResultToJson(int protocol)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"");
            json.Append("result");
            json.Append("\":");
            json.Append("\"" + protocol + "\"");
            json.Append("}");
            return json.ToString();
        }

        public static string ResultToJson(int protocol,String [] str)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"");
            json.Append("result");
            json.Append("\":");
            json.Append("\"" + protocol + "\"");
            json.Append(",\"data" + "\":");
            String ss = "";
            foreach (string s in str) {
                ss = ss + s;
            }
            json.Append(ss.Substring(0,ss.Length-1));
            json.Append("}");
            json.Append("}");
            return json.ToString();
        }

        public static string ResultToJson(string str)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{\"");
            json.Append("result");
            json.Append("\":");
            json.Append("\"" + str + "\"");
            json.Append("}");
            return json.ToString();
        }

    }
}
